<template>
  <div style="background-color: #FFFFFF; padding: 20px;">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

      <el-form-item label="所属项目" prop="project">
        <el-select
          style="width: 100%;"
          v-model="optionsProjectId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入项目名称"
          :remote-method="getProjectInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in project_info"
            :key="index"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="所属模块" prop="module">
        <el-select
          style="width: 100%;"
          :disabled="!optionsProjectId.length"
          v-model="optionsModuleId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入"
          :remote-method="getModuleInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in module_info"
            :key="index"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="所属页面" prop="module_page">
        <el-select
          style="width: 100%;"
          :disabled="!optionsModuleId.length"
          v-model="optionsModulePageId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入"
          :remote-method="getModulePageInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in module_page_info"
            :key="index"
            :label="item.title"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="所属步骤" prop="steps">
        <el-select
          style="width: 100%;"
          :disabled="!optionsModulePageId.length"
          v-model="optionsStepsId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入步骤名称"
          :remote-method="getStepsInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in steps_info"
            :key="index"
            :label="item.title"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="执行函数" prop="selenium_func">
        <el-select
          style="width: 100%;"
          :disabled="!optionsModulePageId.length"
          v-model="optionsSeleniumFuncId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入函数名称"
          :remote-method="getSeleniumFuncInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in selenium_func_info"
            :key="index"
            :label="item.title"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="明细标题" prop="title">
        <el-input v-model="ruleForm.title"></el-input>
      </el-form-item>

      <el-form-item label="KEY" prop="obj_key">
        <el-input v-model="ruleForm.obj_key" style="width: 90%;float: left;"></el-input>
        <div style="width: 10%;float: right;">
          <el-button type="primary" @click="getRandomString()">获取</el-button>
        </div>
      </el-form-item>

      <el-form-item
        v-if="ruleForm.selenium_func_info.sql_detail_body"
        label="查询sql模板"
        prop="into_sql_push">
        <el-input disabled
                  v-model="ruleForm.selenium_func_info.sql_detail_body.sql"
                  type="textarea"
                  :autosize="{ minRows: 2}"></el-input>
      </el-form-item>

      <el-form-item
        v-if="ruleForm.selenium_func_info.sql_detail_body"
        label="查询sql入参"
        prop="into_sql_push">
        <el-input :disabled="!ruleForm.selenium_func_info.sql_detail_body.status" v-model="ruleForm.into_sql_push" type="textarea" :autosize="{ minRows: 2, maxRows: 6}"></el-input>
      </el-form-item>

      <el-form-item
        v-if="ruleForm.selenium_func_info.sql_detail_assert"
        label="断言sql模板" prop="into_sql_push">
        <el-input disabled
                  v-model="ruleForm.selenium_func_info.sql_detail_assert.sql"
                  type="textarea"
                  :autosize="{ minRows: 2}"></el-input>
      </el-form-item>

      <el-form-item
        v-if="ruleForm.selenium_func_info.sql_detail_assert"
        label="断言sql入参">
        <el-input :disabled="!ruleForm.selenium_func_info.sql_detail_assert.status" v-model="ruleForm.assert_sql_push" type="textarea" :autosize="{ minRows: 2, maxRows: 6}"></el-input>
      </el-form-item>

      <el-form-item label="函数入参模板" prop="into_template">
        <el-input v-model="ruleForm.into_template" type="textarea" :autosize="{ minRows: 2, maxRows: 6}"></el-input>
      </el-form-item>

      <el-form-item label="校验出参模板" prop="out_template">
        <el-input v-model="ruleForm.out_template" type="textarea" :autosize="{ minRows: 2, maxRows: 6}"></el-input>
      </el-form-item>

      <el-form-item label="断言出参模板" prop="assert_template">
        <el-input v-model="ruleForm.assert_template" type="textarea" :autosize="{ minRows: 2, maxRows: 6}"></el-input>
      </el-form-item>

      <el-form-item label="函数入参数据" prop="into_data">
        <el-input disabled v-model="ruleForm.into_data" type="textarea"
                  :autosize="{ minRows: 2, maxRows: 6}"></el-input>
      </el-form-item>

      <el-form-item label="查询sql出参" prop="into_sql_pull">
        <el-input disabled v-model="ruleForm.into_sql_pull" type="textarea"
                  :autosize="{ minRows: 2, maxRows: 6}"></el-input>
      </el-form-item>

      <el-form-item label="断言sql出参" prop="assert_sql_pull">
        <el-input disabled v-model="ruleForm.assert_sql_pull" type="textarea"
                  :autosize="{ minRows: 2, maxRows: 6}"></el-input>
      </el-form-item>

      <el-form-item label="页面数据" prop="page_out_data">
        <el-input disabled v-model="ruleForm.page_out_data" type="textarea" :autosize="{ minRows: 2, maxRows: 6}"></el-input>
      </el-form-item>

      <el-form-item label="被校验数据" prop="out_data">
        <el-input disabled v-model="ruleForm.out_data" type="textarea" :autosize="{ minRows: 2, maxRows: 6}"></el-input>
      </el-form-item>

      <el-form-item label="断言数据" prop="assert_data">
        <el-input disabled v-model="ruleForm.assert_data" type="textarea"
                  :autosize="{ minRows: 2, maxRows: 6}"></el-input>
      </el-form-item>

      <el-form-item label="对比结果" prop="result_data">
        <el-input disabled v-model="ruleForm.result_data" type="textarea"
                  :autosize="{ minRows: 2, maxRows: 6}"></el-input>
      </el-form-item>

      <el-form-item label="测试结果显示" prop="show_status">
        <el-switch
          style="float: left;"
          v-model="ruleForm.show_status"
          active-text="显示全部"
          inactive-text="显示错误信息">
        </el-switch>
      </el-form-item>

      <el-form-item label="执行状态">
        <el-select v-model="ruleForm.results" disabled placeholder="请选择" style="width: 100%;">
          <el-option
            v-for="(item,index) in optionsResults"
            :key="index"
            :label="item.value"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="步骤顺序">
        <el-select v-model="ruleForm.sort" placeholder="请选择" style="width: 100%;">
          <el-option
            v-for="(item,index) in optionsSort"
            :key="index"
            :label="item.value"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="ruleForm.status" placeholder="请选择状态" style="width: 100%;">
          <el-option
            v-for="(item,index) in optionsCommon"
            :key="index"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm()">保存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>
<script>
  export default {
    name: 'EditStepsRealize',
    data() {
      return {
        action_host: this.$http.HOST,
        optionsCommon: [],  // 存储CommonChoices实际数据的列表
        optionsResults: [],  // 执行状态
        optionsSort: [],  // 步骤顺序
        // projects: [], // 项目列表，获取1000条项目数据
        EditUrl: this.$http.StepsRealizeURL,  // get编辑的URL
        EditIdStr: 'steps_realize_id',  // 模块id字符串
        pushObjName: 'StepsRealize',  // 成功后跳转

        // 其它表数据,搜索到的全部数据
        project_info: [], // 项目列表，获取1000条项目数据
        module_info: [], // 模块列表，获取1000条项目数据
        module_page_info: [], // 页面列表
        steps_info: [], // 模块列表，获取1000条项目数据
        selenium_func_info: [], // 页面列表

        // 编辑和添加选中的数据id
        loading: false,
        optionsProjectId: [],
        optionsModuleId: [],
        optionsModulePageId: [],
        optionsStepsId: [],
        optionsSeleniumFuncId: [],

        ruleForm: {
          title: '',
          obj_key: '',
          // label_element: '',
          steps: '',
          selenium_func: '',

          into_sql_push: '',
          into_sql_pull: '',
          assert_sql_push: '',
          assert_sql_pull: '',

          into_template: '',
          out_template: '',
          assert_template: '',
          into_data: '',
          out_data: '',
          page_out_data: '',
          assert_data: '',
          result_data: '',
          sort: '',
          results: 3,
          show_status: false,
          // test_status: false,
          // label_location: '',
          status: '',

          steps_info: '',
          selenium_func_info: '',
          project_info: '',
          module_info: '',
          module_page_info: '',

        },  //
        rules: {
          title: [
            {required: true, message: '请输入标题', trigger: 'blur'},
            {min: 1, max: 64, message: '长度在 1 到 64 个字符', trigger: 'blur'}
          ],
        }
      };
    },
    created() {
      this.getEditData();   // get回将要编辑的数据
      this.optionsCommon = this.getChoices.common; // 获取标准CHOICES
      this.optionsResults = this.getChoices.results;
      this.optionsSort = this.getChoices.sort;
    },
    methods: {
      submitForm() {
        this.$myTool.submitForm(this)
      },

      // get回将要编辑的数据
      getEditData() {
        this.$myTool.getEditData(this);
      },

      // 获取随机字符串
      getRandomString() {
        let queryDict = {
          str_len: 16
        };
        this.$myTool.getRandomString(this, queryDict)
      },

      // 项目信息搜索
      getProjectInfo(query) {
        this.$myTool.getObjList(this, this.$http.projectURL, this.project_info, query)
      },

      // 获取模块信息
      getModuleInfo(query) {
        let QuerySet = {
          module_project: this.ruleForm.project,
          inputObjName: query
        };
        this.$myTool.getObjList(this, this.$http.moduleURL, this.module_info, QuerySet)
      },

      // 获取页面信息
      getModulePageInfo(query) {
        let QuerySet = {
          module_page_module_id: this.ruleForm.module,
          module_page_project_id: this.ruleForm.project,
          inputObjName: query
        };
        this.$myTool.getObjList(this, this.$http.ModulePageURL, this.module_page_info, QuerySet)
      },

      // 获取步骤信息
      getStepsInfo(query) {
        let QuerySet = {
          module_page_module_id: this.ruleForm.module,
          module_page_project_id: this.ruleForm.project,
          inputObjName: query
        };
        this.$myTool.getObjList(this, this.$http.StepsURL, this.steps_info, QuerySet)
      },

      // 获取函数信息
      getSeleniumFuncInfo(query) {
        let QuerySet = {
          module_page_module_id: this.ruleForm.module,
          module_page_project_id: this.ruleForm.project,
          module_page_id: this.ruleForm.module_page,
          inputObjName: query
        };
        this.$myTool.getObjList(this, this.$http.SeleniumFuncURL, this.selenium_func_info, QuerySet)
      },

      // get将要编辑的数据
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
    },
    // 监听
    watch: {

      'ruleForm.project_info': function (value) {
        if (value) {
          this.optionsProjectId.push(value.id);
          this.project_info.push(value);
        } else {
          this.optionsProjectId = [];
          this.project_info = [];
        }
      },
      optionsProjectId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
        this.ruleForm.project = value[0]
      },

      'ruleForm.module_info': function (value) {
        if (value) {
          this.optionsModuleId.push(value.id);
          this.module_info.push(value);
        } else {
          this.optionsModuleId = [];
          this.module_info = [];
        }
      },
      optionsModuleId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
        this.ruleForm.module = value[0]
      },

      'ruleForm.module_page_info': function (value) {
        if (value) {
          this.optionsModulePageId.push(value.id);
          this.module_page_info.push(value);
        } else {
          this.optionsModulePageId = [];
          this.module_page_info = [];
        }
      },
      optionsModulePageId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
        this.ruleForm.module_page = value[0]
      },

      'ruleForm.steps_info': function (value) {
        if (value) {
          this.optionsStepsId.push(value.id);
          this.steps_info.push(value);
        } else {
          this.optionsStepsId = [];
          this.steps_info = [];
        }
      },
      optionsStepsId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
        this.ruleForm.steps = value[0]
      },

      'ruleForm.selenium_func_info': function (value) {
        if (value) {
          this.optionsSeleniumFuncId.push(value.id);
          this.selenium_func_info.push(value);
        } else {
          this.optionsSeleniumFuncId = [];
          this.selenium_func_info = [];
        }
      },
      optionsSeleniumFuncId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
        this.ruleForm.selenium_func = value[0]
      },
    },
    // 计算属性
    computed: {
      getChoices() {
        return this.$store.state.choicesInfo
      }
    },

  }
</script>
